package com.zxy.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zxy.common.BindingResultUtil;
import com.zxy.common.ResultData;
import com.zxy.dto.ReportInfo;
import com.zxy.entity.Report;
import com.zxy.mapper.ReportMapper;
import com.zxy.service.IReportService;
import lombok.AllArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * 各学院报道指南
 * </p>
 *
 * @author Zxy
 * @since 2021-05-11
 */
@RestController
@RequestMapping("/report")
@AllArgsConstructor
public class ReportController {

    private final IReportService reportService;
    private final ReportMapper reportMapper;

    // 查询所有学院报道指南信息
    @GetMapping("/findlist")
    public ResultData allReport(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size) {
        Page<ReportInfo> page = new Page<>(current,size);
        IPage<ReportInfo> reportInfoIPage = reportMapper.findAll(page);
        if (reportInfoIPage != null) {
            return ResultData.success(reportInfoIPage);
        } else {
            return ResultData.fail("数据获取异常，请联系网站管理员");
        }
    }


    // 新增学院报道指南
    @PostMapping("/addreport")
    public ResultData addReport(@Validated @RequestBody Report report, BindingResult result){
        BindingResultUtil.validate(result);
        report.setTime(DateUtil.now());
        boolean save = reportService.save(report);
        if (save){
            return ResultData.success(save);
        }else {
            return ResultData.fail("保存失败，请联系网站管理员");
        }
    }
}
